import { createStore } from 'vuex';

import { ref } from 'vue';

const store = createStore({
  state: {
    user: null,
    permissions: [],
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user;
    },
    SET_PERMISSIONS(state, permissions) {
      state.permissions = permissions;
    },
  },
  actions: {
    login({ commit }, user) {
      commit('SET_USER', user);
    },
    setPermissions({ commit }, permissions) {
      commit('SET_PERMISSIONS', permissions);
    },
  },
});

 // 定义角色状态
 const currentRole = ref('');

 // 设置角色的方法
 const setRole = (role) => {
   currentRole.value = role;
 };

 // 导出状态和方法
 const roleStore = {
   currentRole,
   setRole
 };

 // 导出角色权限配置
 const rolePermissions = {
   'purchaser': ['getPurchaseOrders', 'postPurchaseOrders', 'getWarehouseInventory', 'putWarehouseInventory', 'postWarehouseTransfers'],
   'salesperson': ['getSalesProducts', 'putSalesProductPrice'],
   'deliveryman': ['postDeliveryOrders'],
   'admin': ['getPurchaseOrders', 'postPurchaseOrders', 'getWarehouseInventory', 'putWarehouseInventory', 'postWarehouseTransfers', 'getSalesProducts', 'putSalesProductPrice', 'postDeliveryOrders', 'getTraceProduct']
 };

 // 导出角色权限检查方法
 const checkPermission = (permission) => {
   return rolePermissions[currentRole.value]?.includes(permission);
 };

 // 导出角色相关内容
 const roleModule = {
   roleStore,
   rolePermissions,
   checkPermission
 };

 // 导出角色模块
 export { roleModule };
 export default store;